package com.xiaoshuidi.cloud.module.rooms.mq.listener;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.ReadingOrderImportVO;
import com.xiaoshuidi.cloud.module.rooms.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomSdiotReadingOrderMapper;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomSdiotReadingOrderRecordMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotReadingOrder;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotReadingOrderRecord;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class ReadingOrderImportListener extends AnalysisEventListener<ReadingOrderImportVO> {

    private final List<ReadingOrderImportVO> dataList= new ArrayList<>();
    private final RoomSdiotReadingOrderMapper roomSdiotReadingOrderMapper;
    private final RoomSdiotReadingOrderRecordMapper roomSdiotReadingOrderRecordMapper;



    @Override
    public void invoke(ReadingOrderImportVO readingOrderImportVO, AnalysisContext analysisContext) {
        // 将读取到的数据添加到集合中
        dataList.add(readingOrderImportVO);

    }

    @SneakyThrows
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        for (ReadingOrderImportVO readingOrderImportVO : dataList) {
            String roomName = readingOrderImportVO.getRoomName();
            String feeType = readingOrderImportVO.getFeeType();
            BigDecimal currentReadingCount = readingOrderImportVO.getCurrentReadingCount();
            LocalDate currentReadingTime = readingOrderImportVO.getCurrentReadingTime();
            LocalDate dueDate = readingOrderImportVO.getDueDate();
            if(roomName==null || feeType==null || currentReadingCount==null || currentReadingTime==null || dueDate==null){
                throw new ServiceException(ErrorCodeConstants.READING_ORDER_IMPORT_NOT_NULL.getCode(),roomName+ErrorCodeConstants.READING_ORDER_IMPORT_NOT_NULL.getMsg());
            }
            QueryWrapper<RoomSdiotReadingOrder> qw=new QueryWrapper<>();
            qw.eq("room_name",roomName).eq("fee_type",feeType);
            RoomSdiotReadingOrder order = roomSdiotReadingOrderMapper.selectOne(qw);
            if(ObjectUtil.isEmpty(order)){
              throw new ServiceException(ErrorCodeConstants.READING_ORDER_IMPORT.getCode(),roomName+ErrorCodeConstants.READING_ORDER_IMPORT.getMsg());
            }
            RoomSdiotReadingOrderRecord record = new RoomSdiotReadingOrderRecord();
            record.setReadingOrderId(order.getId());
            record.setFeeType(order.getFeeType());
            record.setPrice(order.getPrice());
            LocalDate lastTimeReadingTime = order.getLastTimeReadingTime();
            record.setLastTimeReadingTime(lastTimeReadingTime);
            record.setLastTimeReadingCount(order.getLastTimeReadingCount());
            record.setCurrentReadingTime(currentReadingTime);
            record.setCurrentReadingCount(currentReadingCount);
            BigDecimal lastTimeReadingCount = order.getLastTimeReadingCount();
            BigDecimal reading =null;
                int result = currentReadingCount.compareTo(lastTimeReadingCount);
                if(result >=0){
                     reading = currentReadingCount.subtract(lastTimeReadingCount);
                }else {
                    throw new ServiceException(ErrorCodeConstants.READING_ORDER_IMPORT_COUNT.getCode(),roomName+ErrorCodeConstants.READING_ORDER_IMPORT_COUNT.getMsg());
                }
            record.setReading(reading);
            record.setDueDate(dueDate);
            record.setFee(reading.multiply(order.getPrice()));
            roomSdiotReadingOrderRecordMapper.insert(record);
            order.setLastTimeReadingTime(currentReadingTime);
            order.setLastTimeReadingCount(currentReadingCount);
            roomSdiotReadingOrderMapper.updateById(order);
        }

    }

    public ReadingOrderImportListener(RoomSdiotReadingOrderMapper roomSdiotReadingOrderMapper, RoomSdiotReadingOrderRecordMapper roomSdiotReadingOrderRecordMapper){
        this.roomSdiotReadingOrderMapper=roomSdiotReadingOrderMapper;
        this.roomSdiotReadingOrderRecordMapper=roomSdiotReadingOrderRecordMapper;
    }
}
